package com.lgl.allpeoplevent.ventIndex.controller;

import com.github.pagehelper.PageInfo;
import com.lgl.allpeoplevent.base.controller.BaseController;
import com.lgl.allpeoplevent.util.ControllerUtil;
import com.lgl.allpeoplevent.ventIndex.service.VentLikeService;
import com.lgl.allpeoplevent.ventIndex.service.VentService;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Tools;
import com.lgl.scsapi.vent.model.Vent;
import com.lgl.scsapi.vent.model.VentUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 吐槽controller
 */
@Slf4j
@RestController
@RequestMapping(value="/vent")
public class VentController extends BaseController {
    @Autowired
    private VentService ventService;
    @Autowired
    private VentLikeService ventLikeService;
    /**
     * 保存吐槽数据
     */
    @RequestMapping(value="/saveVent")
    public Object saveVent(@ModelAttribute Vent v){
        if (v == null){
            return -2;
        }
        VentUser ventUser = getSessionUser();
        v.setUserId(ventUser.getUserId());
        return ventService.saveVent(v);
    }
    //保存点赞数据
    @RequestMapping(value="/saveVentLike")
    public Object saveVentLike(@RequestParam Long ventId){
        PageData vlPageData = new PageData(4);
        VentUser ventUser = getSessionUser();
        if (ventUser == null){
            vlPageData.put("status", -1);
            return vlPageData;
        }
        vlPageData.put("ventId", ventId);
        vlPageData.put("likeUserId", ventUser.getUserId());
        ventLikeService.saveVentLike(vlPageData);
        ventService.likeTotalAddOne(ventId);
        PageData resultPd = new PageData(2);
        resultPd.put("status",0);
        return resultPd;
    }
    //获取“我的主页”中的主页访问次数数据
    @RequestMapping(value="/countVentTotalByUser")
    public Object countVentTotalByUser(@RequestParam Integer year){
        PageData countPd = ControllerUtil.getCountParam(getSessionUser(),year);
        List<PageData> ventTotalList = ventService.countVentTotalByUser(countPd);
        return ControllerUtil.countResult(countPd,ventTotalList);
    }
    /**
     * 获取我的吐槽分页对象
     */
    @RequestMapping(value="/pageMyVent")
    public PageInfo pageMyVent(){
        PageData ventParamPd = getPageData();
        int pageNo = ventParamPd.getInt("pageNo"),pageSize = ventParamPd.getInt("pageSize");
        Tools.initPageNoAndSize(pageNo,pageSize);
        ventParamPd.put("ventUserId", getSessionUser().getUserId());
        return ventService.pageMyVent(pageNo,pageSize,ventParamPd);
    }
    //删除吐槽数据
    @RequestMapping(value="/delVentById")
    public Object delVentById(){
        PageData delVentPd = new PageData(4),returnPd = new PageData(2);
        String delVentId = getRequest().getParameter("delVentId");
        delVentPd.put("delUserId", getSessionUser().getUserId());
        delVentPd.put("delVentId", delVentId);
        try {
            ventService.delVentById(delVentPd);
        }catch (Exception e){
            log.error("删除失败：" + delVentPd,e);
        }
        returnPd.put("status",1);
        return returnPd;
    }

}